package cn.le.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *Thread类中包含的成员变量代表了线程的某些优先级。如Thread.MIN_PRIORITY（常数1），
 Thread.MAX_PRIORITY（常数10），Thread.NORM_PRIORITY（常数5）。
 其中每个线程的优先级都在Thread.MIN_PRIORITY到Thread.MAX_PRIORITY（常数10）之间，
 在默认情况下优先级都是Thread.NORM_PRIORITY（常数5）。每个先产生的线程都继承福线程的优先级。
 *
 */
public class SimplePriorities implements Runnable {

    private int countDown = 5;
    private volatile double d;
    private int priority;

    public SimplePriorities(int priority)
    {
        this.priority = priority;
    }
    public String toString()
    {
        //返回对当前正在执行的线程对象的引用。
        return Thread.currentThread()+":"+countDown;
    }
    public void run()
    {
        //设置优先级
        Thread.currentThread().setPriority(priority);
        while (true)
        {
            for (int i = 1; i < 10000; i++)
            {
                d += (Math.PI + Math.E) / i;
                if(i % 1000 == 0)
                {
                    Thread.yield();
                }
            }
            System.out.println(this);
            if(--countDown == 0)
            {
                return;
            }
        }

    }

    public static void main(String[] args) {

        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++)
        {
            exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
            exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
            //exec.shutdown();
        }

    }

}










